﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.BoMonService;
namespace eTMS.Controllers{
public class BoMonControllerBase : CommonController{
	public BoMonControllerBase()
		: base()
	{	
		this.dao = new BoMonServiceClient();
	}
	~BoMonControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private BoMonEntity oBoMon;	
	public BoMonEntity BoMonObject{
		get{
			return oBoMon;
		}
		set{
			oBoMon=value;
		}
	}
	protected BoMonServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSBoMon(false);			
			return _Table;
		}
	}
	public void LoadAllDSBoMon(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oBoMon==null)return false;
		bool result=true;
		try{
			oBoMon = dao.SaveRet(oBoMon);
		}
		catch{
			result=false;
		}
		if(result){
			_ListBoMon.Add(oBoMon);
		}
		return result;
	}
	public bool Update(){
		if(oBoMon==null)return false;
		return dao.Update(oBoMon);
	}
	public bool Delete(){
		if(oBoMon==null)return false;
		bool retvalue = dao.Delete(oBoMon);
		if(retvalue){			
			_ListBoMon.Remove(oBoMon);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 BoMon_ID){
		bool retvalue = dao.DeleteByID(BoMon_ID);		
		if(retvalue){
			BoMonEntity oDeleted=FindBoMon(BoMon_ID);
			if(oDeleted!=null){
				_ListBoMon.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oBoMon = new BoMonEntity();
	}
	//Check if exists MaBoMon
	public bool CheckExistsMaBoMon(object value){	
		return dao.SelectOneByMaBoMon(value.ToString()).MaBoMon!=null;
    }    
    //Copy data from data row in grid to object
    public BoMonEntity MapDataRowToObject(DataRow row){
		BoMonEntity o = new BoMonEntity();
		 o.MaBoMon=Convert.ToString(row["MaBoMon"]);
		 o.TenBoMon=Convert.ToString(row["TenBoMon"]);
		 o.MoTaBoMon=Convert.ToString(row["MoTaBoMon"]);
		 if(!row.IsNull("Khoa_ID"))
			o.Khoa_ID=(Int32)(row["Khoa_ID"]);
		 if(!row.IsNull("BoMon_ID"))
		    o.BoMon_ID=(Int32)(row["BoMon_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(BoMonEntity o,DataRow row){
		row["MaBoMon"]=o.MaBoMon;
		row["TenBoMon"]=o.TenBoMon;
		row["MoTaBoMon"]=o.MoTaBoMon;
		row["Khoa_ID"]=o.Khoa_ID;
		row["BoMon_ID"]=o.BoMon_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.BoMonObject,row);
    }
	public void LoadObject(Int32 BoMon_ID){
		BoMonEntity oLoad=FindBoMon(BoMon_ID);
		if(oLoad==null){
			oBoMon=this.LoadBoMon(BoMon_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oBoMon.IsNullBoMon_ID;
			if(!bNotIsNull){
				this.ListBoMon.Add(oBoMon);
			}
		}
		else{
			oBoMon=oLoad;
		}
	}
	#region CachedList BoMon
	private static List<BoMonEntity> _ListBoMon=new List<BoMonEntity>();
    public List<BoMonEntity>ListBoMon{
		get{
			return _ListBoMon;
		}
	}
	public static List<BoMonEntity>ListBoMonStatic{
		get{
			return _ListBoMon;
		}
	}
	public BoMonEntity FindBoMon(Int32 BoMon_ID){
		BoMonEntity oResult=this.ListBoMon.Find(delegate(BoMonEntity oFind){
			bool result=true; 
			result&=oFind.BoMon_ID.Equals(BoMon_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllBoMon(bool clearCache){
		if(_ListBoMon.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		BoMonServiceClient dao = new BoMonServiceClient();
		_ListBoMon=new List<BoMonEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public BoMonEntity LoadBoMon(Int32 BoMon_ID){			
		BoMonEntity o=dao.SelectOne(BoMon_ID);		
		return o;
	}
	#endregion
}
}
